package com.rao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rao.entity.Role;
import com.rao.entity.UserRoleRelation;
import com.rao.mapper.RoleMapper;
import com.rao.mapper.UserRoleRelationMapper;
import com.rao.service.IUserRoleRelationService;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 后台用户和角色关系表 服务实现类
 * </p>
 *
 * @author rao3030
 * @since 2021-10-09
 */
@RestController//open-feign
public class UserRoleRelationServiceImpl extends ServiceImpl<UserRoleRelationMapper, UserRoleRelation> implements IUserRoleRelationService {

    @Resource
    UserRoleRelationMapper userRoleRelationMapper;
    @Resource
    RoleMapper roleMapper;

    @Override
    public List<Role> queryByUserId(Long userId) {
        QueryWrapper<UserRoleRelation> userRoleRelationQueryWrapper = new QueryWrapper<>();
        userRoleRelationQueryWrapper.lambda().eq(UserRoleRelation::getUserId, userId);
        //先查找用户拥有的所有角色id
        List<UserRoleRelation> userRoleRelations = userRoleRelationMapper.selectList(userRoleRelationQueryWrapper);
        //返回所有的角色
        List<Long> roleIds = userRoleRelations.stream().map(UserRoleRelation::getRoleId).collect(Collectors.toList());
        return roleMapper.selectBatchIds(roleIds);
    }
}
